#include <iostream>
#include <string>
#include <cstring>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;

  typedef struct request    
  {    
    int left;    
    int right;    
    char op;     
  }request_t;    
      
  typedef struct response    
  {    
    int code;    
    int result;    
  }response_t;  

int main(int argc , char* argv[])
{
  if (argc != 3)
  {
    cerr << "usage error" << endl;
    exit(1);
  }

  string ip = argv[1];
  int port = atoi(argv[2]);


  int sockfd = socket(AF_INET , SOCK_STREAM , 0);
  if (sockfd < 0)
  {
    cerr << "socket error" << endl;
    exit(2);
  }


  struct sockaddr_in peer;
  memset(&peer , '\0' , sizeof(peer));

  peer.sin_family = AF_INET;
  peer.sin_port = htons(port);
  peer.sin_addr.s_addr = inet_addr(ip.c_str());

  // connect 
  if (connect(sockfd , (struct sockaddr*)&peer , sizeof(peer)) < 0)
  {
    cerr << "connect error" << endl;
    exit(3);
  }

  while(true)
  {
    request_t rq;
    cout << "请输入左操作数#" ;
    cin >> rq.left;
    cout << "请输出右操作数#" ;
    cin >> rq.right;
    cout << "请输出操作符#" ;
    cin >> rq.op;
    write(sockfd , &rq , sizeof(rq));


    response_t rp;
    read(sockfd , &rp , sizeof(rp)) ;
    cout << "code: " << rp.code << endl;
    cout << "result:" << rp.result << endl;
  }
  return 0;
}
